from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad
import binascii

# AES解密函数
def aes_decrypt(ciphertext, key, iv):
    # 将十六进制字符串转换为字节
    ciphertext = binascii.unhexlify(ciphertext)

    # 初始化AES解密器
    cipher = AES.new(key, AES.MODE_CBC, iv)

    # 解密数据并去除填充
    decrypted_data = unpad(cipher.decrypt(ciphertext), AES.block_size)

    return decrypted_data.decode('utf-8')

# 示例数据
ciphertext = "9e58a1d233c25776c613a575e202e58e9bb27193e929ffa218be29e1d89d6869450b63d3a15851488effaeff2b15a55620f6a93d810a42385bcb76b4645d9fde68a82f9a7f6ded6a9cacf6eaae313cbacac0e4db3a90215f81650853c1f3a7b7669661e403aa18e055deb2392cfb5d630c7bc28a326bd4fb437123950e76fe8670bbaacd67a48ada6f853ce290dc7099c74b10bea615ec1397991847c8164f49a25a56b86ba4c217d8a053f3cc4ab00e9f636a450a58814c2ecee8acf5b123cb16df7b04dd50d3c76623f7b12657e6ff"
key = b'glt6h61ta7kisow7'  # AES密钥，16/24/32字节
iv = b'4hrivgw5s342f9b2'  # CBC模式下的初始化向量，16字节

# 解密
try:
    decrypted_text = aes_decrypt(ciphertext, key, iv)
    print("解密后的文本:", decrypted_text)
except Exception as e:
    print("解密失败:", str(e))
